<?php
/*
---------------------------------------------------------
														+
名称:易通支付通道										+
														+
控制器名称:YiTongController								+
														+
日期:2017-10-11											+
														+
作者:小翁												+
														+
---------------------------------------------------------
*/
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

class JuheController extends PayController{
    protected $config;
    protected $key;
    protected $url;

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);
        $isweixin = is_weixin();
        $ismobile = isMobile();

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>'14']);
        }

        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));

        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>'014']);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $data = array();
        //交易金额
        $data['amount'] = $return['amount'];
        //业务商品描述
        $data['busi_description'] = 'zf';
        //业务商品号
        $data['busi_good_no'] = '100';
        //业务订单号
        $data['busi_order_no'] = $return['orderid'];
        //业务回传参数
        $data['busi_temp_info'] = 'zf';
        //业务用户号
        $data['busi_user_no'] = 'SHTTDZ';
        //IP
        $data['client_ip'] = $_SERVER['REMOTE_ADDR'];

        switch($product['paytype']){
            //微信
            case 1:
                //商户通道号
                $data['merchant_channel_no'] = '5b58adc6-0bbc-44a5-a64c-863f2955fe1e';
                break;
            //微信H5
            case 2:
                //商户通道号
                $data['merchant_channel_no'] = '32e19eb4-963d-48c1-a1a5-04801d8dccfd';
                break;
            //支付宝
            case 3:
                //支付宝H5
            case 4:
                //商户通道号
                $data['merchant_channel_no'] = 'c884bbda-83d7-43ae-885d-cd6d6ed8d455';
                break;
            //QQ钱包扫码
            case 8:
                //商户通道号
                $data['merchant_channel_no'] = '';
                break;
            //网银直连
            case 6:
                //商户通道号
                $data['merchant_channel_no'] = 'f5ec7638-a0eb-44bd-b64a-cc1e12dafef9';
                break;
            default:
                $this->showmessage('暂不支持此方式!',['code'=>'014']);

        }
        //商户号
        $data['merchant_no'] = $this->config['mch_id'];
        //回调地址
        $data['notify_url'] = $return['notifyurl'];
		//$data['notify_url'] = "http://www.dlbp4.cn/Pay_Juhe_notifyurl";

        $str = $this->config['signkey'] . $data['merchant_no'] . $data['amount'] . $data['merchant_channel_no'] . $data['notify_url'] . $data['client_ip'] . $data['busi_user_no'] . $data['busi_order_no'] . $data['busi_good_no'] . $data['busi_description'];
        $data['sign'] = md5($str);

        function curl($apiurl,$data){
            $con = curl_init($apiurl);
            curl_setopt($con, CURLOPT_HEADER, false);
            curl_setopt($con, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($con, CURLOPT_POST, true);
            curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($con, CURLOPT_TIMEOUT, 10);
            curl_setopt($con, CURLOPT_HTTPHEADER,  array(
                "Content-type: application/json;charset='utf-8'",
                "Accept: application/json",
                "Cache-Control: no-cache",
                "Pragma: no-cache",
            ));
            $res = curl_exec($con);
            curl_close($con);

            return $res;
        }

        $res = $origin = curl($this->config['gateway'], $data);

        if(!$res){
			echo $origin;exit;
            $this->showmessage('请求失败!',['code'=>'014']);
        }

        //echo $res."<br/>";

        //保存请求返回
        M("Order")->where([ 'pay_orderid'=>$return['orderid']])->save(["rd_return"=>$res,"request"=>json_encode($data)]);

        $res = json_decode($res, true);
        if(empty($res)){
			//echo '<pre/>'; print_r($data);
            //echo $origin;exit;
            $this->showmessage("数据获取错误", ['code'=>'014-1']);
        }

        //echo '<pre/>'; print_r($res); exit;

        $code = $res['qrcode_url'];

        if( $ismobile && ($product['paytype']==3 || $product['paytype']==4 || $product['paytype']==2)){
            header("location: {$code}");  exit;
        }

        if(!$code){
            $this->showmessage("获取二维码失败", ['code'=>'014']);
        }

        $paytype = transArr( C("PAYTYPES"), "id");

        $this->assign("wap_url", $code);
        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);
        $this->assign("imgurl", C("website.DOMAIN")."/Pay_Qrcode_getCode?data=".urlencode($code));
		
		switch($this->config['paytype']){
            //微信
            case 1:
				if(isMobile()){
					$this->display("WeiXin/weixinwap");
				}else{
					$this->display("WeiXin/weixin");
				}
                break;
            //支付宝扫码
            case 3:
            //支付宝H5
            case 4:
                if(isMobile()){
                    $this->display("WeiXin/alipaywap");
                }else{
                    $this->display("WeiXin/alipay");
                }
                break;
            //支付宝
            case 17:
                $this->display("WeiXin/alipay");
                break;
            //支付宝H5
            case 18:
                $this->display("WeiXin/alipay");
                break;
            default:
                $this->display("WeiXin/Pay");
        }
    }

    //通知
    public function notifyurl(){
        //file_put_contents("notify/juhe.txt", file_get_contents('php://input')."\r\n\r\n", FILE_APPEND);
        //file_put_contents("notify/juhe.txt", getIP()."\r\n\r\n", FILE_APPEND);
        //file_put_contents("notify/juhe.txt", date('Y-m-d H:i:s',time())."\r\n\r\n", FILE_APPEND);

        $s = file_get_contents('php://input');
        $p = $data = json_decode($s, true);
        if(!$p || empty($p)){
            exit("wrong");
        }
        //获取订单信息
        $orderid = $p['busi_order_no'];
        $find = M("Order")->where(array("pay_orderid"=>$orderid))->find();

        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$p['busi_order_no']))->save(array("server_return"=> $s,"pay_orderid_rd"=>$p['trans_no']));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>'Juhe'))->find();

        if($p['trans_flag'] == 100150002 || $p['trans_flag'] == 100150000){
            echo "pail_fail"; exit;
        }

        $request = json_decode($find['request'], true);

        $sign = $p['sign'];

        $str = $this->config['signkey'].$request['merchant_no'].$p['amount'].$request['merchant_channel_no'].$request['notify_url'].$request['client_ip'].$p['busi_user_no'].$p['busi_order_no'].$p['busi_good_no'].$p['busi_description'];
        $sign2 = md5($str);


        if($sign != $sign2){
            exit("fail");
        }

        //处理支付
        //$this->EditMoney($find['pay_orderid'],"Juhe",0);
        //回调成功
        //echo 'SUCCESS'; exit;
        $this->async($find['pay_orderid'],'SUCCESS');
    }

    public function callBackUrl(){

        echo '支付成功'; exit;
    }
}